<template>
  <view class="container">
    <view class="heads">
      <image class="heads_image" src="/static/heads.png" mode="scaleToFill"></image>
    </view>
    <view class="text">
      销售工具是为了销售结果的达成或销售进度过程的管控，起到让销售行为作用力延伸和放大的效果，提高销售效率，倍增销售结果而需要的硬件工具或办公软件工具
    </view>
    <!-- <button @tap="getUserinfo" class="mini-btn" type="primary">授权</button> -->
    <!-- <button @tap="auth" class="mini-btn" type="primary">授权登录</button> -->
    <button v-if="!checkSession" @tap="getUserProfile" class="mini-btn" type="primary"> 授权登录 </button>
    <!-- <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">shoujihao1</button> -->
  </view>
</template>

<script>
export default {
  data() {
    return {
      checkSession: true,
    }
  },
  onShow() {
    this.auth()
  },
  methods: {
    getPhoneNumber(e) {
      console.log(e);
    },
    getUserProfile() {
      let _this = this;
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
      // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        lang: 'zh_CN',
        success: (item) => {
          uni.login({
            provider: 'weixin',
            success: function (res) {
              _this.$post('/sale/wx/login', { jsCode: res.code, encrypteData: item.encryptedData, iv: item.iv, ...item.userInfo }, true).then(() => {
                _this.getUserInfo()
              })
            }
          });
        }
      })
    },
    getUserInfo() {
      let _this = this;
      _this.$get('/sale/wx/get_user_info', true).then(res => {
        console.log(res.data);
        _this.$store.dispatch('user/ChangeUserinfo', res.data).then(() => {
          uni.switchTab({
            url: '/pages/tabbar/schedule'
          });
        })
      }).catch(() => {
        _this.checkSession = false
      })
    },
    auth() {
      let _this = this;
      uni.checkSession({
        success() {
          _this.checkSession = true
          _this.getUserInfo()
          //session_key 未过期，并且在本生命周期一直有效
        },
        fail() {
          _this.checkSession = false
          // session_key 已经失效，需要重新执行登录流程
        }
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  height: 100vh;
  padding: 20px;
  font-size: 14px;
  line-height: 24px;
  display: flex;
  flex-direction: column;
}
.heads {
  width: 120px;
  height: 120px;
  padding: 10px;
  border: 1px solid #ddd;
  box-shadow: 0 0 6px 6px #ddd;
  border-radius: 50%;
  margin: 100px auto 0 auto;
}
.heads_image {
  width: 100px;
  height: 100px;
}
.text {
  padding-top: 50px;
  text-indent: 2em;
}
.mini-btn {
  margin-top: 20px;
  width: 200rpx;
}
</style>
